package com.dji.compose_learning.navigation

import android.widget.Toast
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import androidx.navigation.navDeepLink

enum class PageRoute {
    Main,
    Count,
    Summary,
    Other
}

data class PageItem(
    val pageName: String,
)

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun NavigationPageView() {
    MaterialTheme {
        val navController = rememberNavController()
        var currentScreen by rememberSaveable {
            mutableStateOf(0)
        }
        var pages = listOf(
            PageItem(PageRoute.Main.name),
            PageItem(PageRoute.Count.name),
            PageItem(PageRoute.Summary.name),
            PageItem(PageRoute.Other.name)
        )
        Scaffold(topBar = {
            SwitchRow(selectIndex = currentScreen, tabs = listOf("首页", "统计", "综合","其他"), onItemClick = {
                currentScreen = it
                when (it) {
                    2 -> {
                        navController.navigate(pages[it].pageName+"/hello")
                    }
                    3 -> {
                        navController.navigate(pages[it].pageName+"/James")
                    }
                    else -> {
                        navController.navigate(pages[it].pageName)
                    }
                }
            })
        }) { innerPadding ->
            MyNavHost(navController = navController, modifier = Modifier.padding(innerPadding))
        }


    }
}

var inof = 0
@Composable
fun MyNavHost(navController: NavHostController, modifier: Modifier = Modifier) {
    val backStackEntry = navController.currentBackStackEntryAsState()
    val context = LocalContext.current
    NavHost(navController = navController,
        startDestination = PageRoute.Main.name,
        modifier = modifier) {
        composable(route = PageRoute.Main.name) {
            MainPage()
            //显示当前页名称
//            inof++
//            Toast.makeText(context,
//                backStackEntry.value?.destination?.route+"$inof",
//                Toast.LENGTH_SHORT).show()
        }
        composable(route = PageRoute.Count.name) {
            CountPage()
        }
        composable(route = PageRoute.Summary.name+"/{name}", arguments = listOf(
            navArgument("name"){
                type = NavType.StringType
            }

        )) {
            val name = it.arguments?.getString("name")
            SummaryPage(name?:"")
        }
        // adb shell am start -d "buobao://thisplaces/Jack" -a android.intent.action.VIEW
        composable(route = PageRoute.Other.name+"/{name}",
            arguments = listOf(
                navArgument("name") {
                    type = NavType.StringType
                },
            ),
            deepLinks = listOf(navDeepLink {
                uriPattern = "buobao://thisplaces/{name}"
            })) {
            val name = it.arguments?.getString("name")
            SummaryPage(name?:"")
        }
    }
}

@Composable
fun MainPage() {
    val context = LocalContext.current
    Surface(modifier = Modifier.fillMaxSize()) {
        Text(text = "this is main page.", color = MaterialTheme.colorScheme.primary)
        inof++
        Toast.makeText(context,"$inof",Toast.LENGTH_SHORT).show()
    }
}

@Composable
fun CountPage() {
    Surface(modifier = Modifier.fillMaxSize()) {
        Text(text = "this is count page.", color = MaterialTheme.colorScheme.primary)
    }
}

@Composable
fun SummaryPage(name:String="default") {
    Surface(modifier = Modifier.fillMaxSize()) {
        Text(text = "my name is $name", color = MaterialTheme.colorScheme.primary)
    }
}

@Composable
fun SwitchRow(selectIndex: Int, tabs: List<String>, onItemClick: (Int) -> Unit) {
    Row(modifier = Modifier
        .fillMaxWidth()
        .background(MaterialTheme.colorScheme.primary),
        horizontalArrangement = Arrangement.SpaceBetween) {
        tabs.forEachIndexed { index, it ->
            Text(text = it,
                textAlign = TextAlign.Center,
                modifier = Modifier.width(0.dp).weight(1f).clickable {
                    onItemClick(index)
                }.padding(10.dp),
                color = if (selectIndex == index) MaterialTheme.colorScheme.onPrimary else MaterialTheme.colorScheme.onPrimary.copy(
                    alpha = 0.4f))
        }
    }
}


























